﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Windows;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Mono.Cecil;
using NDasm.Core.UI;
using NDasm.Core.UI.MethodDasm;

namespace NDasm.Addin.UI
{
    [Guid("463f350c-2c65-4f2e-943f-a12ef8971ae5")]
    public class NDasmWindow : ToolWindowPane
    {
        private DasmViewModel dasmViewModel;

        public NDasmWindow()
        {
            // will fail if no Content after constructor
            base.Content = new DasmView();
        }

        public event Action<NDasmWindow> Closed;
        public string AssemblyPath { get; private set; }

        public void Initialize(ModuleDefinition module, IMemberDefinition member = null)
        {
            AssemblyPath = module.FullyQualifiedName;
            if (dasmViewModel != null)
                dasmViewModel.Dispose();
            dasmViewModel = new DasmViewModel(module);
            ((DasmView)base.Content).DataContext = dasmViewModel;
            Caption = String.Format("{0} - NDasm", module.Name);
            if (member != null)
                dasmViewModel.ProcessMember(member);
        }

        public void Show()
        {
            ((IVsWindowFrame)Frame).Show();    
        }

        protected override void OnClose()
        {
            base.OnClose();            
            if (Closed != null)
                Closed(this);
        }        
    }
}
